package com.kerrykidz.system.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.common.service.ICommonService;
import com.kerrykidz.system.dao.model.GblSecBranEntity;
import com.kerrykidz.system.dao.model.SecRole2usrEntity;
import com.kerrykidz.system.dao.model.SecUsrEntity;
import com.kerrykidz.system.dao.model.WfmAuthorizeEntity;
import com.kerrykidz.system.service.IUserService;
import com.kerrykidz.system.service.IWfmAuthorizeService;
import com.kerrykidz.system.util.DateUtil;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.kerrykidz.workflow.dao.model.ActivitiEntity;
import com.rdp.framework.exception.RDPException;

@Controller
@RequestMapping("WfmAuthorize")
public class WfmAuthorizeController {

	@Autowired
	private IWfmAuthorizeService iWfmAuthorizeService;

	@Autowired
	private ICommonService commonService;

	// 用户管理
	@Autowired
	private IUserService userService;

	@RequestMapping("toShenP")
	public ModelAndView toShenP() throws RDPException {
		ModelAndView mav = new ModelAndView("system/system_shenp");
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (null == userInfo) {
			return null;
		}
		List<WfmAuthorizeEntity> fromUsers = null;
		String branId = userInfo.getBranId();
		if (branId.indexOf("ZB") == -1)// 非总部，只能看到自己门店的授权
		{
			fromUsers = iWfmAuthorizeService.findFromUsers(branId);
		} else {
			fromUsers = iWfmAuthorizeService.findFromUsers(null);
		}
		List<ActivitiEntity> activList = iWfmAuthorizeService.queryProcDef();
		Iterator<ActivitiEntity> i = activList.iterator();
		while(i.hasNext()){
			ActivitiEntity ae = i.next();
			if (ae.getKey().equalsIgnoreCase("server")) {
				i.remove();
				mav.addObject("activitiEntity", ae);
			}
		}
		
		if ("ZB".equals(userInfo.getBranId()))// 总部
		{
			mav.addObject("branList", commonService.getBranList(null));
		} else {
			GblSecBranEntity gb = new GblSecBranEntity();
			gb.setBranName(userInfo.getBranName());
			gb.setPkBranId(userInfo.getBranId());
			List<GblSecBranEntity> beanList = new ArrayList<GblSecBranEntity>();
			beanList.add(gb);
			mav.addObject("branList", beanList);
		}
		mav.addObject("fromUsers", fromUsers);
		mav.addObject("activList", activList);
		mav.addObject("brandId", userInfo.getBranId());
		return mav;
	}

	/**
	 * 审批转授权 分页
	 * 
	 * @param wfmAuthorize
	 * @return
	 * @throws RDPException
	 * @throws ParseException
	 */
	@RequestMapping("findWfmAuthorize")
	@ResponseBody
	public WfmAuthorizeEntity findWfmAuthorize(
			@RequestParam(value = "fromStartDateSearch", required = false) String fromStartDateSearch,
			@RequestParam(value = "fromEndDateSearch", required = false) String fromEndDateSearch,
			@RequestParam(value = "toStartDateSearch", required = false) String toStartDateSearch,
			@RequestParam(value = "toEndDateSearch", required = false) String toEndDateSearch,
			WfmAuthorizeEntity wfmAuthorize) throws RDPException,
			ParseException {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
		if (StringUtils.isNotBlank(fromStartDateSearch)) {
			wfmAuthorize.setFromStartDate(sdf.parse(fromStartDateSearch
					+ " 00:00:00"));
		}
		if (StringUtils.isNotBlank(fromEndDateSearch)) {
			wfmAuthorize.setFromEndDate(sdf.parse(fromEndDateSearch
					+ " 23:59:59"));
		}
		if (StringUtils.isNotBlank(toStartDateSearch)) {
			wfmAuthorize.setToStartDate(sdf.parse(toStartDateSearch
					+ " 00:00:00"));
		}
		if (StringUtils.isNotBlank(toEndDateSearch)) {
			wfmAuthorize.setToEndDate(sdf.parse(toEndDateSearch + " 23:59:59"));
		}
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo.getBranId().indexOf("ZB") == -1)// 非总部，只能看到自己门店的授权
		{
			wfmAuthorize.setFkBranId(userInfo.getBranId());
		}
		return iWfmAuthorizeService.findWfmAuthorize(wfmAuthorize);
	}

	/**
	 * 根据临时授权编号， 更新delFlage，取消委托
	 * 
	 * @param wfmAuthorize
	 * @throws RDPException
	 */
	@RequestMapping("cancelAuthorize")
	@ResponseBody
	public String cancelAuthorize(String pkAuthId, String modiBy) {
		UserInfo userInfo = UserUtil.getUserFromSession();
		if (userInfo != null) {
			try {
				iWfmAuthorizeService.cancelWfmAuthorize(pkAuthId,
						userInfo.getUserId());
				return Constants.SUCCESS;
			} catch (RDPException e) {
				return Constants.FAILURE;
			}
		}
		return Constants.FAILURE;
	}

	/**
	 * 根据用户角色获取制定门店用户的详细信息
	 * 
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping(value = "selectUsesJobInfoByRole", method = RequestMethod.POST)
	@ResponseBody
	public List<SecUsrEntity> selectUsesJobInfoByRole(HttpSession session,
			SecUsrEntity entity) throws RDPException {
		return iWfmAuthorizeService.selectUsesJobInfoByRole(entity);
	}

	/**
	 * 获取制定门店的用户信息
	 * 
	 * @param session
	 * @return
	 * @throws RDPException
	 * @date: 2014年9月17日
	 */
	@RequestMapping("selectUsesJobInfo")
	@ResponseBody
	public List<SecUsrEntity> selectUsesJobInfo(HttpSession session,
			SecUsrEntity entity) throws RDPException {
		return userService.selectUsesJobInfo(entity);
	}

	/**
	 * 根据用户id获取用户角色
	 * 
	 * @param userId
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "findUserRoleById", method = RequestMethod.POST)
	@ResponseBody
	public List<SecRole2usrEntity> findUserRoleById(String fkUsrId)
			throws RDPException {
		return iWfmAuthorizeService.findUserRoleById(fkUsrId);
	}

	/**
	 * 新增新的审批转授权
	 * 
	 * @param wfmAuthorize
	 * @throws RDPException
	 */
	@RequestMapping(value = "insertWfmAuthorize", method = RequestMethod.POST)
	@ResponseBody
	public String insertWfmAuthorize(WfmAuthorizeEntity wfmAuthorize) {
		try {
			UserInfo userInfo = UserUtil.getUserFromSession();
			if (null == userInfo) {
				return Constants.FAILURE;
			}
			String strToDate = DateUtil.formatDate("yyyy/MM/dd", wfmAuthorize.getToDate());
			wfmAuthorize.setToDate(DateUtil.getDate(strToDate+" 23:59:59", "yyyy/MM/dd HH:mm:ss"));
			wfmAuthorize.setPkAuthId(PrimaryKeyGenerator.getRandomPrimaryKey());
			wfmAuthorize.setModiBy(userInfo.getUserId());
			wfmAuthorize.setDelFlage("0");
			if(StringUtils.isBlank(wfmAuthorize.getProcDefId()))
				wfmAuthorize.setProcDefId(wfmAuthorize.getIsAllProc());
			iWfmAuthorizeService.insertWfmAuthorize(wfmAuthorize);
			/*List<String> roleList = wfmAuthorize.getRoleIdList();
			if(roleList.size() == 0)
			{
				wfmAuthorize.setFkRoleId(null);//roleId为空表示委托任务
				iWfmAuthorizeService.insertWfmAuthorize(wfmAuthorize);
			}
			else
			{
				for (String roleId : wfmAuthorize.getRoleIdList()) {
					wfmAuthorize.setFkRoleId(roleId);//委托角色
					wfmAuthorize.setPkAuthId(PrimaryKeyGenerator.getRandomPrimaryKey());
					
				}
			}*/
			
			return Constants.SUCCESS;
		} catch (RDPException e) {
			return Constants.FAILURE;
		} 
	}
}
